package studykana.activity;

import java.util.ArrayList;
import java.util.List;

import studykana.adapters.BangChuCaiAdapter;
import studykana.database.AppData;
import studykana.database.Databasehandler;
import studykana.models.Alphabet;
import toan.da.com.vn.R;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;

//Show Alphabet list
public class BangChuCaiActivity extends Activity {

	Databasehandler db;

	private BangChuCaiAdapter mAdapter1;
	private BangChuCaiAdapter mAdapter2;
	private BangChuCaiAdapter mAdapter3;

	ArrayList<Integer> listIdSelected;

	private GridView gvLevel1;
	private GridView gvLevel2;
	private GridView gvLevel3;

	// menu = 0 : Hiragana, menu = 1 : Katakana
	private int menu;

	private CheckBox cbLevel1;
	private CheckBox cbLevel2;
	private CheckBox cbLevel3;
	private Activity activity = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bang_chu_cai_activity);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			menu = bundle.getInt("menu");
			listIdSelected = bundle.getIntegerArrayList("listidselected");
		}
		if (listIdSelected == null) {
			listIdSelected = new ArrayList<Integer>();
		}

		gvLevel1 = (GridView) findViewById(R.id.gridViewLevel1);
		gvLevel2 = (GridView) findViewById(R.id.gridViewLevel2);
		gvLevel3 = (GridView) findViewById(R.id.gridViewLevel3);
		cbLevel2 = (CheckBox) findViewById(R.id.cbLevel2);
		cbLevel3 = (CheckBox) findViewById(R.id.cbLevel3);
		cbLevel1 = (CheckBox) findViewById(R.id.cbLevel1);

		db = new Databasehandler(this);

		prepareGridviewLevel1();
		prepareGridviewLevel2();
		prepareGridviewLevel3();

		gvLevel1.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				mAdapter1.setColorItem(view);
				addIdIntoList(mAdapter1, position, view);
			}
		});
		gvLevel2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				mAdapter2.setColorItem(view);
				addIdIntoList(mAdapter2, position, view);
			}
		});
		gvLevel3.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				mAdapter3.setColorItem(view);
				addIdIntoList(mAdapter3, position, view);
			}
		});

		cbLevel1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				for (int i = 0; i < 46; i++) {
					mAdapter1.setColorItem(gvLevel1.getChildAt(i), isChecked);
				}
				addIdIntoList(mAdapter1, isChecked);
			}
		});
		cbLevel2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				for (int i = 0; i < 25; i++) {
					mAdapter2.setColorItem(gvLevel2.getChildAt(i), isChecked);
				}
				addIdIntoList(mAdapter2, isChecked);
			}
		});

		cbLevel3.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				for (int i = 0; i < 33; i++) {
					mAdapter3.setColorItem(gvLevel3.getChildAt(i), isChecked);
				}
				addIdIntoList(mAdapter3, isChecked);
			}
		});
	}

	// when alphabet is selected, add id of alphabet into list or remove id if
	// it has selected.
	private void addIdIntoList(BangChuCaiAdapter adapter, int position,
			View view) {
		if (((CheckBox) view.findViewById(R.id.checkBox)).isChecked()) {
			listIdSelected.add(adapter.getAlphabetId(position));
		} else {
			listIdSelected.remove((Integer) adapter.getAlphabetId(position));
		}
	}

	private void addIdIntoList(BangChuCaiAdapter adapter, boolean isChecked) {
		// if checkbox = true, get all alphabet, else clear all.
		if (isChecked) {
			for (int i = 0; i < adapter.getCount(); i++) {
				listIdSelected.add(adapter.getAlphabetId(i));
			}
		} else {
			for (int i = 0; i < adapter.getCount(); i++) {
				listIdSelected.remove((Integer) adapter.getAlphabetId(i));
			}
		}
	}

	// check select all, if select all then return true, else return false
	private boolean checkSelectAll(BangChuCaiAdapter adapter) {
		for (int i = 0; i < adapter.getCount(); i++) {
			if (!listIdSelected.contains((int) adapter.getItemId(i)))
				return false;
		}
		return true;
	}

	// prepare list 1
	private void prepareGridviewLevel1() {
		List<Alphabet> list = new ArrayList<Alphabet>();
		for (int i = 0; i < 46; i++) {
			list.add(AppData.listAllAlphabet.get(i));
		}
		mAdapter1 = new BangChuCaiAdapter(list, activity, listIdSelected, menu);
		gvLevel1.setAdapter(mAdapter1);
		// if select all then set checkbox = true.
		if (checkSelectAll(mAdapter1))
			cbLevel1.setChecked(true);
	}

	// prepare list 2
	private void prepareGridviewLevel2() {
		List<Alphabet> list = new ArrayList<Alphabet>();
		for (int i = 46; i < 71; i++) {
			list.add(AppData.listAllAlphabet.get(i));
		}
		mAdapter2 = new BangChuCaiAdapter(list, activity, listIdSelected, menu);
		gvLevel2.setAdapter(mAdapter2);
		if (checkSelectAll(mAdapter2))
			cbLevel2.setChecked(true);
	}

	// prepare list 3
	private void prepareGridviewLevel3() {
		List<Alphabet> list = new ArrayList<Alphabet>();
		for (int i = 71; i < AppData.listAllAlphabet.size(); i++) {
			list.add(AppData.listAllAlphabet.get(i));
		}
		mAdapter3 = new BangChuCaiAdapter(list, activity, listIdSelected, menu);
		gvLevel3.setAdapter(mAdapter3);
		if (checkSelectAll(mAdapter3))
			cbLevel3.setChecked(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// size of listIdSelected least has 5 element.
			if (listIdSelected.size() < 5) {
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				builder.setIcon(android.R.drawable.ic_dialog_alert)
						.setTitle("Thông Báo")
						.setMessage("Bạn phải chọn ít nhất 5 chữ cái để học")
						.setPositiveButton("Đóng", new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int arg1) {
								dialog.cancel();
							}
						});
				builder.show();
			} else {
				Intent newActivity = new Intent(this, HocChuCaiActivity.class);
				newActivity.putExtra("menu", menu);
				newActivity.putIntegerArrayListExtra("listidselected",
						listIdSelected);
				this.startActivity(newActivity);
				this.finish();
			}
			break;

		default:
			break;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// size of listIdSelected least has 5 element.
			if (listIdSelected.size() < 5) {
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				builder.setIcon(android.R.drawable.ic_dialog_alert)
						.setMessage("Bạn phải chọn ít nhất 5 chữ cái để học")
						.setPositiveButton("Đóng", new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int arg1) {
								dialog.cancel();
							}
						});
				builder.show();
			} else {
				Intent newActivity = new Intent(this, HocChuCaiActivity.class);
				newActivity.putExtra("menu", menu);
				newActivity.putIntegerArrayListExtra("listidselected",
						listIdSelected);
				this.startActivity(newActivity);
				this.finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
